# Memento Cache Proxy for Yahoo search engine.
# Author: Ahmed AlSum aalsum@cs.odu.edu
# Date: Sep 16, 2010

import sys, os, re
import urllib
import urllib2
import simplejson
from dateutil import parser as dateparser
from dateutil.tz import *

from baseHandler import *

end_point = 'http://uk.search.yahoo.com/search?p='
boss_end_point = 'http://boss.yahooapis.com/ysearch/web/v1/'
APP_ID = 'wsEyHwHV34Es8ZPrzN6sAN4.CU7mAM5jgdefuKbYGu4lS5reSu5YAcmjzu9u7ww-'

class YahooHandler(BaseProxyHandler):

    def fetch_changes(self, req, requri, dt=None):


        query = requri
        final_url = end_point + query

        user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.10) Gecko/20100504 Firefox/3.5.10 (.NET CLR 3.5.30729)'

        headers = {'User-Agent': user_agent}
        request = urllib2.Request(final_url, None, headers)

        opener = urllib2.build_opener()
        response = opener.open(request)

        the_page = response.read()


        uriExpression = re.compile(r"<a href=\"http://[A-Za-z0-9.:=/%-_ ]*\">Cached</a>")

        result = uriExpression.search(the_page)
        changes = None

        if result:
            cachedDiv = result.group(0)
            loc = cachedDiv[9:len(cachedDiv)-12]

            if check_cache_availability(loc)<>True:
                return changes


            url = boss_end_point + query + '?appid=' + APP_ID + '&count=1&format=json';

            apiResults = simplejson.load(urllib2.urlopen(url))
            

            if 'Error' in apiResults:
                # An error occurred; raise an exception
                return changes
            
            
            searchResponse = apiResults['ysearchresponse']
            if searchResponse['count'] == '1':
                resultSet = searchResponse['resultset_web']
                dtstr = resultSet[0]['date'] +  ' 00:00:00Z'
                dtobj = dateparser.parse(dtstr)
                changes = []
                changes.append((dtobj, loc, {'last' : dtobj, 'obs' : 1, 'type' : 'observed'}))

        return changes


def check_cache_availability(cacheUri):
    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.10) Gecko/20100504 Firefox/3.5.10 (.NET CLR 3.5.30729)'

    headers = {'User-Agent': user_agent}

    request = urllib2.Request(cacheUri, None, headers)

    opener = urllib2.build_opener()
    response = opener.open(request)

    the_page = response.read()


    unfoundText = 'We\'re sorry, but we could not process your request for the cache of'

    if the_page.find(unfoundText) < 0:
        return True
    else:
        return False


def handler(req):
    hdlr = YahooHandler('yahoo')
    return basehandler(req, hdlr)
      